---
title: undefinedable
description: Creates an undefinedable schema.
source: /schemas/undefinedable/undefinedable.ts
contributors:
  - fabian-hiller
---

import { ApiList, Link, Property } from '~/components';
import { properties } from './properties';

# undefinedable

Creates an undefinedable schema.

```ts
const Schema = v.undefinedable<TWrapped, TDefault>(wrapped, default_);
```

## Generics

- `TWrapped` <Property {...properties.TWrapped} />
- `TDefault` <Property {...properties.TDefault} />

## Parameters

- `wrapped` <Property {...properties.wrapped} />
- `default_` {/* prettier-ignore */}<Property {...properties.default_} />

### Explanation

With `undefinedable` the validation of your schema will pass `undefined` inputs, and if you specify a `default_` input value, the schema will use it if the input is `undefined`. For this reason, the output type may differ from the input type of the schema.

> `undefinedable` behaves exactly the same as <Link href="../optional/">`optional`</Link> at runtime. The only difference is the input and output type when used for object entries. While <Link href="../optional/">`optional`</Link> adds a question mark to the key, `undefinedable` does not.

> Note that `undefinedable` does not accept `null` as an input. If you want to accept `null` inputs, use <Link href="../nullable/">`nullable`</Link>, and if you want to accept `null` and `undefined` inputs, use <Link href="../nullish/">`nullish`</Link> instead. Also, if you want to set a default output value for any invalid input, you should use <Link href="../fallback/">`fallback`</Link> instead.

## Returns

- `Schema` <Property {...properties.Schema} />

## Examples

The following examples show how `undefinedable` can be used.

### Undefinedable string schema

Schema that accepts `string` and `undefined`.

```ts
const UndefinedableStringSchema = v.undefinedable(
  v.string(),
  "I'm the default!"
);
```

### Undefinedable date schema

Schema that accepts [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) and `undefined`.

> By using a function as the `default_` parameter, the schema will return a new [`Date`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) instance each time the input is `undefined`.

```ts
const UndefinedableDateSchema = v.undefinedable(v.date(), () => new Date());
```

### Undefinedable entry schema

Object schema with an undefinedable entry.

```ts
const UndefinedableEntrySchema = v.object({
  key: v.undefinedable(v.string()),
});
```

### Unwrap undefinedable schema

Use <Link href="../unwrap/">`unwrap`</Link> to undo the effect of `undefinedable`.

```ts
const UndefinedableNumberSchema = v.undefinedable(v.number());
const NumberSchema = v.unwrap(UndefinedableNumberSchema);
```

## Related

The following APIs can be combined with `undefinedable`.

### Schemas

<ApiList
  items={[
    'any',
    'array',
    'bigint',
    'blob',
    'boolean',
    'custom',
    'date',
    'enum',
    'exactOptional',
    'file',
    'function',
    'instance',
    'intersect',
    'lazy',
    'literal',
    'looseObject',
    'looseTuple',
    'map',
    'nan',
    'never',
    'nonNullable',
    'nonNullish',
    'nonUndefinedable',
    'null',
    'nullable',
    'nullish',
    'number',
    'object',
    'objectWithRest',
    'picklist',
    'promise',
    'record',
    'set',
    'strictObject',
    'strictTuple',
    'string',
    'symbol',
    'tuple',
    'tupleWithRest',
    'undefined',
    'undefinedable',
    'union',
    'unknown',
    'variant',
    'void',
  ]}
/>

### Methods

<ApiList
  items={[
    'assert',
    'config',
    'fallback',
    'getDefault',
    'getDefaults',
    'getFallback',
    'getFallbacks',
    'is',
    'message',
    'parse',
    'parser',
    'pipe',
    'safeParse',
    'safeParser',
    'unwrap',
  ]}
/>

### Actions

<ApiList
  items={[
    'check',
    'brand',
    'description',
    'flavor',
    'metadata',
    'partialCheck',
    'rawCheck',
    'rawTransform',
    'readonly',
    'title',
    'transform',
  ]}
/>

### Utils

<ApiList items={['entriesFromList', 'isOfKind', 'isOfType']} />
